package com.wei.shopping.controller;

import com.wei.shopping.entrty.PageResult;
import com.wei.shopping.entrty.QueryPageBean;
import com.wei.shopping.entrty.Result;
import com.wei.shopping.pojo.Notices;
import com.wei.shopping.service.NoticesService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/notices")
public class NoticesController {

    @Resource
    private NoticesService noticesService;

    @RequestMapping("/findallbyuserid")
    public PageResult findAll(@RequestParam("tpname") String tpname, @RequestBody QueryPageBean queryPageBean, HttpSession session){
        //获取userid
        String userid = (String)session.getAttribute("userid");
        PageResult allByUserId = noticesService.findAllByUserId(queryPageBean, userid,tpname);
        return allByUserId;
    }
    @RequestMapping("/isread")
    public Result isread(@RequestParam("noticesid") String noticesid){
        try{
            //设置为已读并设置为不是最新消息
            noticesService.isread(noticesid);
            return new Result(true,"读取消息成功");
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"读取消息失败");
        }
    }

    /**
     * 获取最新通知
     * @return
     */
    @RequestMapping("/lastnotices")
    public Result lastnotices(HttpSession session){
        try{
            String userid = (String)session.getAttribute("userid");
            List<Notices> lastnotices = noticesService.lastnotices(userid);
            return new Result(true,"查找最新消息成功",lastnotices);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"查找最新消息失败");
        }
    }
}
